import React, { FC, lazy } from 'react'
import { BrowserRouter, Route, Redirect, Switch } from 'react-router-dom'

const loading = () => <div className="loader" />

// Containers
const AppLayout = lazy(() => import('./layouts'))

// pages
const Login = lazy(() => import('@/views/login'))

const App: FC = () => {
  const isProd = process.env.REACT_APP_ENV === 'production'
  const token = 1

  return (
    <BrowserRouter basename={isProd ? '/react-store-admin' : '/react-store-admin'}>
      <React.Suspense fallback={loading()}>
        <Switch>
          <Route exact path="/login" render={(props) => <Login {...props} />} />
          <Route
            path="/"
            render={(props) => {
              if (!token) {
                return <Redirect exact to="/login" {...props} />
              }
              return <AppLayout {...props} />
            }}
          />
          {/* <Redirect from="/" to="/index" /> */}
        </Switch>
      </React.Suspense>
    </BrowserRouter>
  )
}

export default App
